package com.apexsoft.monitor.front.console.user.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.apexsoft.front.common.support.Result;
import com.apexsoft.front.common.utils.annotation.CrudMethod;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apexsoft.monitor.front.console.aop.AopAnnotation;
import com.apexsoft.monitor.front.console.user.om.User;
import com.apexsoft.monitor.front.console.user.service.UserService;
import com.apexsoft.front.common.support.BaseFrontController;
import com.apexsoft.front.common.utils.JsonResult;

@Controller("UserController")
@RequestMapping("/console/user")
public class UserController extends BaseFrontController {
	
	protected final transient Logger log = LogManager.getRootLogger();
	
	@Autowired
	private UserService userSvc = null;
	
	// 跳转到修改密码页面
	@RequestMapping(value = "/goUpdatePwd")
	public String goUpdate(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		String loginId = request.getParameter("loginId");
		JSONObject result = userSvc.getSystemUser(loginId);
		if (result.getBooleanValue("success")) {
			request.setAttribute("user", result.getJSONObject("obj"));
			return "console/user/updatePwd";
		} else {
			this.printString(response, result.getString(Result.NOTE));
			return "redirect:/console/index/main";
		}
	}
	// 跳转到修改用户信息页面
	@RequestMapping(value = "/goUpdateUserInfo")
	public String goUpdateUserInfo(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		String loginId = request.getParameter("loginId");
		JSONObject result = userSvc.getSystemUser(loginId);
		if (result.getBooleanValue("success")) {
			request.setAttribute("user", result.getJSONObject("obj"));
			return "console/user/updateUserInfo";
		} else {
			this.printString(response, result.getString(Result.NOTE));
			return "redirect:/console/index/main";
		}
	}

	// 修改系统用户密码
	@AopAnnotation(description="修改系统用户密码")
	@RequestMapping(value = "/uptSystemUser")
	public String uptSystemUser(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		String id = request.getParameter("id");
		String loginId = request.getParameter("loginId");
		String usertype = request.getParameter("usertype");
		String name = request.getParameter("name");
		String menuIds = request.getParameter("menuIds");
		String oldPwd = request.getParameter("oldPwd");
		String newPwd = request.getParameter("newPwd");
		User u = new User();
		u.setId(Integer.parseInt(id));
		u.setLoginId(loginId);
		u.setName(name);
		u.setMenuIds(menuIds);
		u.setPassword(newPwd);
		u.setUsertype(Integer.parseInt(usertype));
		JSONObject result = userSvc.uptSystemPwd(u,oldPwd);
		this.printJSON(response, result);
		return null;
	}
	// 修改系统用户信息
	@AopAnnotation(description="修改系统用户信息")
	@RequestMapping(value = "/uptSystemUserInfo")
	public String uptSystemUserInfo(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		JSONObject result = new JSONObject();
		String id = request.getParameter("id");
		String loginId = request.getParameter("loginId");
		String name = request.getParameter("name");
		String editable = request.getParameter("editable");
		if(StringUtils.isEmpty(id)) {
			result.put("success", false);
			result.put(Result.NOTE, "参数值不正确");
		}else {
			User u = userSvc.getUserById(Long.parseLong(id));
			u.setName(name);
			JSONObject attrObj = new JSONObject();
			attrObj.put("editable", editable);
			u.setAttr(attrObj.toJSONString());
			result = userSvc.uptSystemInfo(u);
		}
		
		this.printJSON(response, result);
		return null;
	}
	
	// 系统运维人员列表
	@RequestMapping(value = "/list")
	public String list(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
//		JSONObject result = null;
//		result = userSvc.getSystemUserList();
//		request.setAttribute("systemUserList", result.getJSONObject("obj"));
		return "console/user/list";
	}
	
	// 跳转到添加系统运维人员页面
	@CrudMethod
	@RequestMapping(value = "/goAdd")
	public String goAdd(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		return "console/user/add";
	}
	
	// 添加系统运维人员
	@CrudMethod
	@AopAnnotation(description="添加系统运维人员")
	@RequestMapping(value = "/add")
	public String add(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		JSONObject result = new JSONObject();
		String loginId = request.getParameter("loginId");
		String name = request.getParameter("name");
		String editable = request.getParameter("editable");
		if (StringUtils.isNotBlank(loginId) && StringUtils.isNotBlank(name)
				&& StringUtils.isNotBlank(editable)) {
			User user = new User();
			user.setLoginId(loginId);
			user.setName(name);
			JSONObject attrObj = new JSONObject();
			attrObj.put("editable", editable);
			user.setAttr(attrObj.toJSONString());
			//默认密码为6个1
			user.setPassword("96e79218965eb72c92a549dd5a330112");
			user.setUsertype(1);
			result = userSvc.addSystemUser(user);
			request.getSession(false).removeAttribute("token");
		}else{
			result.put("success", false);
			result.put(Result.NOTE, "参数值不正确");
		}
		this.printJSON(response, result);
		return null;
	}
	
	// 删除系统维护人员
	@CrudMethod
	@AopAnnotation(description="删除系统维护人员")
	@RequestMapping(value = "/del")
	public String del(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		String id = request.getParameter("id");
		JSONObject result = userSvc.delSystemUser(id);
		this.printJSON(response, result);
		return null;
	}
	// 系统维护人员的权限管理
	@RequestMapping(value = "/permission")
	public String permission(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		return "console/user/permission";
	}
	// 修改维护人员的权限
	@CrudMethod
	@AopAnnotation(description="修改维护人员的权限")
	@RequestMapping(value = "/updatePermission")
	public String updatePermission(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		String loginId = request.getParameter("loginId");
		String menuIds = request.getParameter("menuIds");
		JSONObject result = null;
		result = userSvc.updatePermission(loginId,menuIds);
		this.printJSON(response, result);
		return null;
	}
	// 加载user的菜单权限树
	@RequestMapping(value = "/loadMenuTree")
	public String loadMenuTree(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		JSONArray result = null;
		String loginId = request.getParameter("loginId");
		result = userSvc.getSystemMenuList(loginId);
		this.printJSON(response, result);
		return null;
	}
	
	@RequestMapping(value = "/listByPageForEasyui")
	public String listByPageForEasyui (HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		String pageStr = request.getParameter("pageNo");
		String pageCountStr = request.getParameter(Result.PAGE_SIZE);
		int page = 0, pageCount = 0;
		JSONObject result = new JSONObject();
		try {
			page = Integer.valueOf(pageStr);
			pageCount = Integer.valueOf(pageCountStr);
			result = userSvc.listByPageForEasyui(page, pageCount).getJSONObject("obj");
		} catch (Exception e) {
			log.error(e.getMessage());
			result = new JsonResult().getExceptionResult(e);
		}
		this.printJSON(response, result);
		return null;
	}

	@RequestMapping(value = "/getAllUsers")
	public String getAllTargets(HttpServletRequest request, HttpServletResponse response) throws Exception {

		JSONArray userArr = new JSONArray();
		JSONObject result = userSvc.getSystemUserList();
		if (result != null && result.getIntValue(Result.CODE) > 0) {
			JSONObject nullObj = new JSONObject();
			nullObj.put("id","");
			nullObj.put("name","----全部----");
			userArr.add(nullObj);
			userArr.addAll(1,result.getJSONArray(Result.RECORDS));
//            targetArr = result.getJSONArray(Result.RECORDS);
		} else {
			log.error("查询运维人员列表失败:"+result==null?"null":result.toString());
		}
		this.printJSON(response, userArr);
		return null;
	}
}
